package com.example.baiduai.service;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.baidu.aip.util.Base64Util;
import com.example.baiduai.factory.AipFactory;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc:
 * @Time 2022/09/24 12:41
 */
public class FaceBodyService {

    /**
     * 人脸检测功能
     * @param faceData
     * @param face_field
     * @return
     */
    public String face(byte[] faceData,String face_field) {
        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("face_field",face_field);
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");
        String image = Base64Util.encode(faceData);
        String imageType = "BASE64";
        // 人脸检测
        AipFace client = AipFactory.getAipFace();
        JSONObject res = client.detect(image, imageType, options);

        return res.toString();
    }

    /**
     * 人脸对比功能
     * @param faceData
     * @return
     */
    public String face_equals(byte[] faceData){
        // image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应。
        String image = Base64Util.encode(faceData);
        MatchRequest req1 = new MatchRequest(image, "BASE64");
        MatchRequest req2 = new MatchRequest(image, "BASE64");
        ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
        requests.add(req1);
        requests.add(req2);
        AipFace client = AipFactory.getAipFace();
        JSONObject res = client.match(requests);
        return res.toString(2);
    }
}
